* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* Balance Table: Description of Clinic and Baseline Community Characteristics  *
* **************************************************************************** *
/*
** Purpose:  Create Balance Table on Clinic and Baseline Community Characteristics


*/
* **************************************************************************** *
* **************************************************************************** *

	use  "${Replicate_SocialSignals_dtaInter}/Balance_ClinicCommunityCharacteristics.dta", clear



	* Drop existing matrices
	cap mat drop _all


	* --------------------------------------------------------------------------
	* Panel A: Clinic characteristics
	local  clinic_vars           "staff_immunization regular stock_out "
	local  launch_vars           "days_launched sensitization_days listing_time HMvisits"
	local  implementation_vars   "food_supplement incentive_bednet immunization_gift1 W01clinic_fee_last day_shift stock_out_bin"

	summ   `clinic_vars' `launch_vars' `implementation_vars'

	* Panel B: Community  characteristics
	local  distanceVars          " dist_close dist_far "
	local  knowledge             " immunization_indicator "
	local  perception            " complaints_6 complaints_3 complaints_4 complaints_1 complaints_2 "

	summ  `distanceVars'  `knowledge' `perception'


	local   treatVar      = " intervention_arm "
	local   strataVar     = " strata "
	local   clusterVar    = " clinic_code "
	local   covariates    = " anc2 anc3 anc4 "


* **************************************************************************** *
* 1.)  Panel A: Clinic characteristics
* **************************************************************************** *
* Baseline characteristics

* ---------------------------------------------------------------------------- *
* Compute ttest  mean differences
* ---------------------------------------------------------------------------- *

	local  baselineVars           "staff_immunization regular stock_out "

	preserve

		iebaltab      ///
		`baselineVars',  ///
		vce(cluster `clusterVar')  ///
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates')  ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)  ///
		fmissok feqtest pftest covarmissok  fnoobs   ${BalStars}  ///
		rowvarlabels onerow  ///
		tblnonote  ///
		browse  ///
		replace

		drop in 1/3

		gen     count = _n

		* Temporary save the table output
		tempfile  ttest_diff
		save   	 `ttest_diff'

	restore


* ---------------------------------------------------------------------------- *
* Compute ttest p-values
* ---------------------------------------------------------------------------- *


	* b) p-values

	preserve

		#d ;
		iebaltab   ///
		`baselineVars' ,  ///
		vce(cluster `clusterVar')
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)
		fmissok feqtest pboth covarmissok   fnoobs   ${BalStars}
		rowvarlabels onerow
		tblnonote
		browse
		replace
		;
		#d	cr

		drop in 1/3
		gen     count = _n

		drop    v2-v5

		foreach variable of numlist 6(1)11 {
		replace v`variable' = "[" + v`variable' + "]"     if v`variable' !=""
		}
		replace count = count +1

		drop if v1 == ""

		* Temporary save the table output
		tempfile  ttest_pvals
		save   	 `ttest_pvals'
	
	restore



* ---------------------------------------------------------------------------- *
* Merge ttest differences & p-values
* ---------------------------------------------------------------------------- *

preserve

		use `ttest_diff', clear

		merge 1:1 count using `ttest_pvals', gen(Merge) keepusing(v6-v11) update


		* Compute Number of Observations and Clinics below each test:
		foreach column in 1 2 3 4 {
		local   col_1 = `column'+1
		gen     n`column' = v`col_1'           if inlist(v1,"N","Clusters")
		}

		foreach column in 1 2 3 4  {
		destring n`column', replace
		}

		* Generate new columns for difference calculations:
		foreach column of numlist 6(1)11 {
		gen      n`column' = .
		}

		* Compute the new N's:
		replace  n6  = n1+n2
		replace  n7  = n1+n3
		replace  n8  = n1+n4
		replace  n9  = n2+n3
		replace  n10 = n2+n4
		replace  n11 = n3+n4

		* String and replace:
		foreach column of numlist 6(1)11 {
		tostring n`column', replace
		}

		foreach column of numlist 6(1)11 {
		replace v`column' = n`column'               if n`column' !="." & v`column' ==""
		}

		foreach column of numlist 2(1)5 {
		replace v`column' = ""                      if v`column' !="" & inlist(v1,"N","Clusters")
		}

		* Keep only the Control Group Mean:
		drop v3-v5
		drop n1-n11

		drop count Merge

		replace v1 =  "Observations"         if v1 == "N"
		replace v1 =  "Clinics"              if v1 == "Clusters"

		drop if v1 == "Clinics"

		local    baseline_fileNum = 0

		dataout, save("${Replicate_SocialSignals_ATables}/baseline_balance_`baseline_fileNum'.tex") ///
		replace tex nohead noauto

	restore



* **************************************************************************** *
* 2.) Distance Variables: Experiment implementation
* **************************************************************************** *

* ---------------------------------------------------------------------------- *
* Compute ttest  mean differences
* ---------------------------------------------------------------------------- *

	local  experimentVars  =  "days_launched sensitization_days listing_time HMvisits"


	preserve

		iebaltab      ///
		`experimentVars',  ///
		vce(cluster `clusterVar')  ///
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates')  ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)  ///
		fmissok feqtest pftest covarmissok  fnoobs   ${BalStars}  ///
		rowvarlabels onerow  ///
		tblnonote  ///
		browse  ///
		replace

		drop in 1/3

		gen     count = _n

		* Temporary save the table output
		tempfile  ttest_diff
		save   	 `ttest_diff'

	restore


* ---------------------------------------------------------------------------- *
* Compute ttest p-values
* ---------------------------------------------------------------------------- *


	* b) p-values

	preserve

		#d ;
		iebaltab   ///
		`experimentVars',  ///
		vce(cluster `clusterVar')
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)
		fmissok feqtest pboth covarmissok   fnoobs   ${BalStars}
		rowvarlabels onerow
		tblnonote
		browse
		replace
		;
		#d	cr

		drop in 1/3
		gen     count = _n

		drop    v2-v5

		foreach variable of numlist 6(1)11 {
		replace v`variable' = "[" + v`variable' + "]"     if v`variable' !=""
		}
		replace count = count +1

		drop if v1 == ""

		* Temporary save the table output
		tempfile  ttest_pvals
		save   	 `ttest_pvals'
		
	restore



* ---------------------------------------------------------------------------- *
* Merge ttest differences & p-values
* ---------------------------------------------------------------------------- *

	preserve

		use `ttest_diff', clear

		merge 1:1 count using `ttest_pvals', gen(Merge) keepusing(v6-v11) update


		* Compute Number of Observations and Clinics below each test:
		foreach column in 1 2 3 4 {
		local   col_1 = `column'+1
		gen     n`column' = v`col_1'           if inlist(v1,"N","Clusters")
		}

		foreach column in 1 2 3 4  {
		destring n`column', replace
		}

		* Generate new columns for difference calculations:
		foreach column of numlist 6(1)11 {
		gen      n`column' = .
		}

		* Compute the new N's:
		replace  n6  = n1+n2
		replace  n7  = n1+n3
		replace  n8  = n1+n4
		replace  n9  = n2+n3
		replace  n10 = n2+n4
		replace  n11 = n3+n4

		* String and replace:
		foreach column of numlist 6(1)11 {
		tostring n`column', replace
		}

		foreach column of numlist 6(1)11 {
		replace v`column' = n`column'               if n`column' !="." & v`column' ==""
		}

		foreach column of numlist 2(1)5 {
		replace v`column' = ""                      if v`column' !="" & inlist(v1,"N","Clusters")
		}

		* Keep only the Control Group Mean:
		drop v3-v5
		drop n1-n11

		drop count Merge

		replace v1 =  "Observations"         if v1 == "N"
		replace v1 =  "Clinics"              if v1 == "Clusters"

		drop if v1 == "Clinics"

		local    experiment_fileNum = 0

		dataout, save("${Replicate_SocialSignals_ATables}/experiment_balance_`experiment_fileNum'.tex") ///
		replace tex nohead noauto

	restore



* **************************************************************************** *
* 3.) Service indicators
* **************************************************************************** *

* ---------------------------------------------------------------------------- *
* Compute ttest  mean differences
* ---------------------------------------------------------------------------- *


	local  implementationVars   "food_supplement incentive_bednet immunization_gift1 W01clinic_fee_last day_shift stock_out_bin"


	preserve

		iebaltab      ///
		`implementationVars',  ///
		vce(cluster `clusterVar')  ///
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates')  ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)  ///
		fmissok feqtest pftest covarmissok  fnoobs   ${BalStars}  ///
		rowvarlabels onerow  ///
		tblnonote  ///
		browse  ///
		replace

		drop in 1/3

		gen     count = _n

		* Temporary save the table output
		tempfile  ttest_diff
		save   	 `ttest_diff'

	restore


* ---------------------------------------------------------------------------- *
* Compute ttest p-values
* ---------------------------------------------------------------------------- *


	* b) p-values

	preserve

		#d ;
		iebaltab   ///
		`implementationVars' ,  ///
		vce(cluster `clusterVar')
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)
		fmissok feqtest pboth covarmissok   fnoobs   ${BalStars}
		rowvarlabels onerow
		tblnonote
		browse
		replace
		;
		#d	cr

		drop in 1/3
		gen     count = _n

		drop    v2-v5

		foreach variable of numlist 6(1)11 {
		replace v`variable' = "[" + v`variable' + "]"     if v`variable' !=""
		}
		replace count = count +1

		drop if v1 == ""

		* Temporary save the table output
		tempfile  ttest_pvals
		save   	 `ttest_pvals'
		
	restore



* ---------------------------------------------------------------------------- *
* Merge ttest differences & p-values
* ---------------------------------------------------------------------------- *

	preserve

		use `ttest_diff', clear

		merge 1:1 count using `ttest_pvals', gen(Merge) keepusing(v6-v11) update


		* Compute Number of Observations and Clinics below each test:
		foreach column in 1 2 3 4 {
		local   col_1 = `column'+1
		gen     n`column' = v`col_1'           if inlist(v1,"N","Clusters")
		}

		foreach column in 1 2 3 4  {
		destring n`column', replace
		}

		* Generate new columns for difference calculations:
		foreach column of numlist 6(1)11 {
		gen      n`column' = .
		}

		* Compute the new N's:
		replace  n6  = n1+n2
		replace  n7  = n1+n3
		replace  n8  = n1+n4
		replace  n9  = n2+n3
		replace  n10 = n2+n4
		replace  n11 = n3+n4

		* String and replace:
		foreach column of numlist 6(1)11 {
		tostring n`column', replace
		}

		foreach column of numlist 6(1)11 {
		replace v`column' = n`column'               if n`column' !="." & v`column' ==""
		}

		foreach column of numlist 2(1)5 {
		replace v`column' = ""                      if v`column' !="" & inlist(v1,"N","Clusters")
		}

		* Keep only the Control Group Mean:
		drop v3-v5
		drop n1-n11

		drop count Merge

		replace v1 =  "Observations"         if v1 == "N"
		replace v1 =  "Clinics"              if v1 == "Clusters"

		drop if v1 == "Clinics"

		local    implementation_fileNum = 0

		dataout, save("${Replicate_SocialSignals_ATables}/implementation_balance_`implementation_fileNum'.tex") ///
						 replace tex nohead noauto

	restore




* **************************************************************************** *
* 4.) Distance Variables: Close
* **************************************************************************** *

	local  Dist_Close     = " dist_close "


* ---------------------------------------------------------------------------- *
* Compute ttest  mean differences
* ---------------------------------------------------------------------------- *

	* (1) ttest mean differences
	preserve

		iebaltab  `Dist_Close',  ///
		vce(cluster `clusterVar') ///
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4) ///
		fmissok feqtest pftest covarmissok    fnoobs   ${BalStars} ///
		rowvarlabels onerow ///
		tblnonote ///
		browse ///
		replace

		drop in 1/3

		gen     count = _n

		* Temporary save the table output
		tempfile  ttest_diff
		save     `ttest_diff'

	restore


* ---------------------------------------------------------------------------- *
* Compute ttest p-values
* ---------------------------------------------------------------------------- *

 *  p-values
 
	preserve

		#d ;
		iebaltab   `Dist_Close',
		vce(cluster `clusterVar')
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)
		fmissok feqtest pboth covarmissok   fnoobs   ${BalStars}
		rowvarlabels onerow
		tblnonote
		browse
		replace
		;
		#d	cr

		drop in 1/3
		gen     count = _n

		drop    v2-v5

		foreach    variable of numlist 6(1)11 {
		replace v`variable' = "[" + v`variable' + "]"     if v`variable' !=""
		}
		replace count = count +1

		drop if v1 == ""

		* Temporary save the table output
		tempfile  ttest_pvals
		save   	 `ttest_pvals'
		
	restore



* ---------------------------------------------------------------------------- *
* Merge ttest differences & p-values
* ---------------------------------------------------------------------------- *

preserve

		use `ttest_diff', clear

		merge 1:1 count using `ttest_pvals', gen(Merge) keepusing(v6-v11) update


		* Compute Number of Observations and Clinics below each test:
		foreach column in 1 2 3 4 {
		local   col_1 = `column'+1
		gen     n`column' = v`col_1'           if inlist(v1,"N","Clusters")
		}

		foreach column in 1 2 3 4  {
		destring n`column', replace
		}

		* Generate new columns for difference calculations:
		foreach column of numlist 6(1)11 {
		gen      n`column' = .
		}

		* Compute the new N's:
		replace  n6  = n1+n2
		replace  n7  = n1+n3
		replace  n8  = n1+n4
		replace  n9  = n2+n3
		replace  n10 = n2+n4
		replace  n11 = n3+n4

		* String and replace:
		foreach column of numlist 6(1)11 {
		tostring n`column', replace
		}

		foreach column of numlist 6(1)11 {
		replace v`column' = n`column'               if n`column' !="." & v`column' ==""
		}

		foreach column of numlist 2(1)5 {
		replace v`column' = ""                      if v`column' !="" & inlist(v1,"N","Clusters")
		}

		* Keep only the Control Group Mean:
		drop v3-v5
		drop n1-n11

		drop count Merge

		replace v1 = "Observations"         if v1 == "N"
		replace v1 = "Clinics"              if v1 == "Clusters"


		local    distclose_fileNum = 0

		dataout, save("${Replicate_SocialSignals_ATables}/distclose_balance_`distclose_fileNum'.tex") ///
		         replace tex nohead noauto
	restore



* **************************************************************************** *
* 5.) Distance Variables: Far
* **************************************************************************** *

	local   Dist_Far     = " dist_far "

* ---------------------------------------------------------------------------- *
* Compute ttest  mean differences
* ---------------------------------------------------------------------------- *

		* (1) ttest mean differences
	preserve

		iebaltab  `Dist_Far',  ///
		vce(cluster `clusterVar') ///
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4) ///
		fmissok feqtest pftest covarmissok    fnoobs   ${BalStars} ///
		rowvarlabels onerow ///
		tblnonote ///
		browse ///
		replace

		drop in 1/3

		gen     count = _n

		* Temporary save the table output
		tempfile  ttest_diff
		save     `ttest_diff'
	
	restore

* ---------------------------------------------------------------------------- *
* Compute ttest p-values
* ---------------------------------------------------------------------------- *

   *  p-values
   preserve

		#d ;
		iebaltab   `Dist_Far',
		vce(cluster `clusterVar')
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)
		fmissok feqtest pboth covarmissok   fnoobs   ${BalStars}
		rowvarlabels onerow
		tblnonote
		browse
		replace
		;
		#d	cr

		drop in 1/3
		gen     count = _n

		drop    v2-v5

		foreach    variable of numlist 6(1)11 {
		replace v`variable' = "[" + v`variable' + "]"     if v`variable' !=""
		}
		replace count = count +1

		drop if v1 == ""

		* Temporary save the table output
		tempfile  ttest_pvals
		save   	 `ttest_pvals'
		
   restore



* ---------------------------------------------------------------------------- *
* Merge ttest differences & p-values
* ---------------------------------------------------------------------------- *

	preserve

		use `ttest_diff', clear

		merge 1:1 count using `ttest_pvals', gen(Merge) keepusing(v6-v11) update


		* Compute Number of Observations and Clinics below each test:
		foreach column in 1 2 3 4 {
		local   col_1 = `column'+1
		gen     n`column' = v`col_1'           if inlist(v1,"N","Clusters")
		}

		foreach column in 1 2 3 4  {
		destring n`column', replace
		}

		* Generate new columns for difference calculations:
		foreach column of numlist 6(1)11 {
		gen      n`column' = .
		}

		* Compute the new N's:
		replace  n6  = n1+n2
		replace  n7  = n1+n3
		replace  n8  = n1+n4
		replace  n9  = n2+n3
		replace  n10 = n2+n4
		replace  n11 = n3+n4

		* String and replace:
		foreach column of numlist 6(1)11 {
		tostring n`column', replace
		}

		foreach column of numlist 6(1)11 {
		replace v`column' = n`column'               if n`column' !="." & v`column' ==""
		}

		foreach column of numlist 2(1)5 {
		replace v`column' = ""                      if v`column' !="" & inlist(v1,"N","Clusters")
		}

		* Keep only the Control Group Mean:
		drop v3-v5
		drop n1-n11

		drop count Merge

		replace v1 = "Observations"         if v1 == "N"
		replace v1 = "Clinics"              if v1 == "Clusters"


		local    distfar_fileNum = 0

		dataout, save("${Replicate_SocialSignals_ATables}/distfar_balance_`distfar_fileNum'.tex") ///
						 replace tex nohead noauto
	restore



* **************************************************************************** *
* 6.) Community Knowledge Indicator
* **************************************************************************** *

	local   KnowledgeVars = " immunization_indicator "

* ---------------------------------------------------------------------------- *
* Compute ttest  mean differences
* ---------------------------------------------------------------------------- *

		* (1) ttest mean differences
		preserve

			iebaltab  ///
			`KnowledgeVars',  ///
			vce(cluster `clusterVar') ///
			grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
			grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4) ///
			fmissok feqtest pftest covarmissok    fnoobs   ${BalStars} ///
			rowvarlabels onerow ///
			tblnonote ///
			browse ///
			replace

			drop in 1/3

			gen     count = _n

			* Temporary save the table output
			tempfile  ttest_diff
			save     `ttest_diff'
			
		restore

* ---------------------------------------------------------------------------- *
* Compute ttest p-values
* ---------------------------------------------------------------------------- *

   *  p-values
   preserve

		#d ;
		iebaltab  ///
		`KnowledgeVars',  ///
		vce(cluster `clusterVar')
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)
		fmissok feqtest pboth covarmissok   fnoobs   ${BalStars}
		rowvarlabels onerow
		tblnonote
		browse
		replace
		;
		#d	cr

		drop in 1/3
		gen     count = _n

		drop    v2-v5

		foreach    variable of numlist 6(1)11 {
		replace v`variable' = "[" + v`variable' + "]"     if v`variable' !=""
		}
		replace count = count +1

		drop if v1 == ""

		* Temporary save the table output
		tempfile  ttest_pvals
		save   	 `ttest_pvals'
		
   restore



* ---------------------------------------------------------------------------- *
* Merge ttest differences & p-values
* ---------------------------------------------------------------------------- *

	preserve

		use `ttest_diff', clear

		merge 1:1 count using `ttest_pvals', gen(Merge) keepusing(v6-v11) update


		* Compute Number of Observations and Clinics below each test:
		foreach column in 1 2 3 4 {
		local   col_1 = `column'+1
		gen     n`column' = v`col_1'           if inlist(v1,"N","Clusters")
		}

		foreach column in 1 2 3 4  {
		destring n`column', replace
		}

		* Generate new columns for difference calculations:
		foreach column of numlist 6(1)11 {
		gen      n`column' = .
		}

		* Compute the new N's:
		replace  n6  = n1+n2
		replace  n7  = n1+n3
		replace  n8  = n1+n4
		replace  n9  = n2+n3
		replace  n10 = n2+n4
		replace  n11 = n3+n4

		* String and replace:
		foreach column of numlist 6(1)11 {
		tostring n`column', replace
		}

		foreach column of numlist 6(1)11 {
		replace v`column' = n`column'               if n`column' !="." & v`column' ==""
		}

		foreach column of numlist 2(1)5 {
		replace v`column' = ""                      if v`column' !="" & inlist(v1,"N","Clusters")
		}

		* Keep only the Control Group Mean:
		drop v3-v5
		drop n1-n11

		drop count Merge

		replace v1 = "Observations"         if v1 == "N"
		replace v1 = "Clinics"              if v1 == "Clusters"


		local    knowledge_fileNum = 0

		dataout, save("${Replicate_SocialSignals_ATables}/knowledge_balance_`knowledge_fileNum'.tex") ///
		replace tex nohead noauto
	
	restore




* **************************************************************************** *
* 7.) Perceptions of reasons for parents to miss vaccines
* **************************************************************************** *

	local   perceptionVars = " complaints_6 complaints_3 complaints_4 complaints_1 complaints_2 "

* ---------------------------------------------------------------------------- *
* Compute ttest  mean differences
* ---------------------------------------------------------------------------- *

* (1) ttest mean differences
	preserve

		iebaltab  `perceptionVars',  ///
		vce(cluster `clusterVar') ///
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4) ///
		fmissok feqtest pftest covarmissok    fnoobs   ${BalStars} ///
		rowvarlabels onerow ///
		tblnonote ///
		browse ///
		replace

		drop in 1/3

		gen     count = _n

		* Temporary save the table output
		tempfile  ttest_diff
		save     `ttest_diff'
	
	restore


* ---------------------------------------------------------------------------- *
* Compute ttest p-values
* ---------------------------------------------------------------------------- *

	*  p-values
	preserve

		#d ;
		iebaltab   `perceptionVars',
		vce(cluster `clusterVar')
		grpvar(`treatVar') fixedeffect(`strataVar') covariates(`covariates') ///
		grplabels(1 Control @ 2 Uninformative @ 3 Signal at 4  @ 4 Signal at 5 ) order(1 2 3 4)
		fmissok feqtest pboth covarmissok   fnoobs   ${BalStars}
		rowvarlabels onerow
		tblnonote
		browse
		replace
		;
		#d	cr

		drop in 1/3
		gen     count = _n

		drop    v2-v5

		foreach    variable of numlist 6(1)11 {
		replace v`variable' = "[" + v`variable' + "]"     if v`variable' !=""
		}
		replace count = count +1

		drop if v1 == ""

		* Temporary save the table output
		tempfile  ttest_pvals
		save   	 `ttest_pvals'
	restore


* ---------------------------------------------------------------------------- *
* Merge ttest differences & p-values
* ---------------------------------------------------------------------------- *

	preserve

		use `ttest_diff', clear

		merge 1:1 count using `ttest_pvals', gen(Merge) keepusing(v6-v11) update


		* Compute Number of Observations and Clinics below each test:
		foreach column in 1 2 3 4 {
		local   col_1 = `column'+1
		gen     n`column' = v`col_1'           if inlist(v1,"N","Clusters")
		}

		foreach column in 1 2 3 4  {
		destring n`column', replace
		}

		* Generate new columns for difference calculations:
		foreach column of numlist 6(1)11 {
		gen      n`column' = .
		}

		* Compute the new N's:
		replace  n6  = n1+n2
		replace  n7  = n1+n3
		replace  n8  = n1+n4
		replace  n9  = n2+n3
		replace  n10 = n2+n4
		replace  n11 = n3+n4

		* String and replace:
		foreach column of numlist 6(1)11 {
		tostring n`column', replace
		}

		foreach column of numlist 6(1)11 {
		replace v`column' = n`column'               if n`column' !="." & v`column' ==""
		}

		foreach column of numlist 2(1)5 {
		replace v`column' = ""                      if v`column' !="" & inlist(v1,"N","Clusters")
		}

		* Keep only the Control Group Mean:
		drop v3-v5
		drop n1-n11

		drop count Merge

		replace v1 = "Observations"         if v1 == "N"
		replace v1 = "Clinics"              if v1 == "Clusters"


		local    perceptions_fileNum = 0

		dataout, save("${Replicate_SocialSignals_ATables}/perceptions_balance_`perceptions_fileNum'.tex") ///
		replace tex nohead noauto
		
	restore




* **************************************************************************** *
* Prepare Final Table
* **************************************************************************** *

	cap  file close _all


	local BalanceTables =  " baseline experiment implementation " +  ///
	                       " distclose distfar knowledge perceptions "


	foreach level of local BalanceTables {

		* Remove lines from `dataout` export
		foreach lineToRemove in "\BSdocumentclass[]{article}"			///
		"\BSsetlength{\BSpdfpagewidth}{8.5in}" 	///
		"\BSsetlength{\BSpdfpageheight}{11in}"  ///
		"\BSbegin{document}" 				       	///
		"\BSend{document}" 					     	///
		"\BSbegin{tabular}{lcccccccc}"			///
		"Variable"								///
		"\BShline"								///
		"\BSend{tabular}"						{

		filefilter "${Replicate_SocialSignals_ATables}/`level'_balance_``level'_fileNum'.tex"      ///
		"${Replicate_SocialSignals_ATables}/`level'_balance_`=``level'_fileNum'+1'.tex" ///
		, from("`lineToRemove'") to("") replace
		erase	     "${Replicate_SocialSignals_ATables}/`level'_balance_``level'_fileNum'.tex"

		local `level'_fileNum = ``level'_fileNum' + 1
	
	}

		* Add incipit and end of LaTeX table
		*(to be directly input in TeX document) without further formatting
		file open  `level'File													///
		using "${Replicate_SocialSignals_ATables}/`level'_balance_``level'_fileNum'.tex"               ///
		, text read

		* Loop over lines of the original TeX file and save everything in a local
		local 	   `level'File ""
		file read  `level'File line
		while r(eof) == 0 {
		local 	  `level'File  " ``level'File' `line' "
		file read `level'File line
		}
		file close `level'File

		* Erase original file
		erase "${Replicate_SocialSignals_ATables}/`level'_balance_``level'_fileNum'.tex"

	}


	dis "`baselineFile'"
	dis "`experimentFile'"
	dis "`implementationFile'"
	dis "`distcloseFile'"
	dis "`distfarFile'"
	dis "`knowledgeFile'"
	dis "`perceptionsFile'"


	* Make final table
	file  open finalFile using "${Replicate_SocialSignals_ATables}/baltab_clinic_community.tex"	///
		, text write replace

	#d	;

		file write finalFile
		"                                       & \multicolumn{1}{c}{(1)}                & \multicolumn{1}{c}{(2)}                          & \multicolumn{1}{c}{(3)}               & \multicolumn{1}{c}{(4)}                & \multicolumn{1}{c}{(5)}                 & \multicolumn{1}{c}{(6)}               & \multicolumn{1}{c}{(7)}  & \multicolumn{1}{c}{(8)}  \\"    _n      ///
		"                                       &                                        & \multicolumn{6}{c}{\textbf{t-test differences}}  & \multicolumn{1}{c}{}  \\"    _n   ///
		"                                       & \multicolumn{1}{c}{\textbf{Control}}   & \multicolumn{6}{c}{\textbf{[p-value]}}           & \multicolumn{1}{c}{}  \\"    _n   ///
		"\multicolumn{1}{l}{\textbf{Variable}}  & \multicolumn{1}{c}{\textbf{Mean/(SE)}} & \multicolumn{1}{c}{\textbf{(C)-(UI)}} & \multicolumn{1}{c}{\textbf{(C)-(S4)}} &  \multicolumn{1}{c}{\textbf{(C)-(S5)}} & \multicolumn{1}{c}{\textbf{(UI)-(S4)}} & \multicolumn{1}{c}{\textbf{(UI)-(S5)}} & \multicolumn{1}{c}{\textbf{(S4)-(S5)}} & \multicolumn{1}{c}{\textbf{F-Test}} \\ \hline \\[-2ex]"    _n    ///
		"\multicolumn{9}{l}{\textbf{\textit{Panel A: Clinic characteristics}}}					 \\ [0.5ex]  " _n
		"\multicolumn{9}{l}{\textbf{\textit{Baseline characteristics}}}									 \\ [0.5ex]  " _n
		"\addlinespace[0.75ex] `baselineFile' 																				   \\ [-2ex]   " _n
		"\multicolumn{9}{l}{\textbf{\textit{Experiment implementation}}}								 \\ [0.5ex]  " _n
		"\addlinespace[0.75ex] `experimentFile' 																				 \\ [-2ex]   " _n
		"\multicolumn{9}{l}{\textbf{\textit{Service indicators collected throughout implementation}}} \\ [0.5ex]  " _n
		"\addlinespace[0.75ex] `implementationFile' 																		 \\ [-2ex]   " _n
		"\multicolumn{9}{l}{\textbf{\textit{Panel B: Community Characteristics}}}			   \\ [0.5ex]  " _n
		"\addlinespace[0.75ex] `distcloseFile' 													                    [-2ex]   " _n
		" `distfarFile' 																			  	                       \\ [-2ex]   " _n
		"\multicolumn{9}{l}{\textbf{\textit{Community knowledge}}}                       \\ [0.5ex]  " _n
		"\addlinespace[0.75ex] `knowledgeFile' 									                         \\ [-2ex]   " _n
		"\multicolumn{9}{l}{\textbf{\textit{Community perceptions}}}                       \\ [0.5ex]  " _n
		"\addlinespace[0.75ex] `perceptionsFile' 								                            [-2ex]   " _n
		"																													      \hline \hline \\ [-2ex] "
		;
	#d	cr

	file close finalFile


********************************************************************************
** End of the Dofile !!!
********************************************************************************
